package org.zhxy.zhxy.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zhxy.zhxy.entity.AppointRecord;
import org.zhxy.zhxy.entity.Notice;
import org.zhxy.zhxy.service.NoticeService;

import org.zhxy.zhxy.utils.PageResult;
import org.zhxy.zhxy.utils.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 项目名称: zhxy
 * 作者: zhaojs
 * 创建日期: 2024年10月20日
 * 文件说明: 见类描述
 */
@RestController
@RequestMapping("/api/admin")
public class AdminNoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 添加公告
     */
    @PostMapping("/notice/add")
    public Result addNotice(@Valid @RequestBody Notice notice) {
        notice.setCreateTime(new Date());
        return noticeService.save(notice) ? Result.success("添加成功") : Result.fail("添加失败");
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/notice/delete")
    public Result deleteNotice(@NotNull(message = "id不能为空") Integer id) {
        return noticeService.removeById(id) ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 编辑公告
     */
    @PostMapping("/notice/update")
    public Result updateNotice(@Valid @RequestBody Notice notice) {
        notice.setUpdateTime(new Date());
        return noticeService.updateById(notice) ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 查询公告
     */
    @GetMapping("/notice/list")
    public Result listNotice(@NotNull(message = "页码不能为空") int page, @NotNull(message = "页码不能为空") int pageSize) {
//        Page<Notice> noticePage = noticeService.query()
//                .page(new Page<>(page, pageSize));
//        PageResult<Notice> pageResult = new PageResult<>(noticePage.getTotal(), noticePage.getRecords());
        Page<Notice> noticePage = noticeService.listNotice(page,pageSize);
        PageResult<Notice> pageResult = new PageResult<>(noticePage.getTotal(), noticePage.getRecords());
        return Result.success(pageResult);
    }
}
